﻿/************************************************************************
 * 窗口自定义绘制的时候自定义的主系统控制栏目                             
 * 用法直接放到窗口页面：其父容器必须为窗体
 * Dock设置为TOP
************************************************************************/


using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Xml;

namespace Mrsoft.Controls
{
    public delegate void MyDlgt(object sender, EventArgs e);
    public partial class SystemControl : Nothing
    {
        public SystemControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 关闭按钮单击响应事件
        /// </summary>
        public event EventHandler CloseBtnClick;
        /// <summary>
        ///最小化按钮的单击响应事件
        /// </summary>
        public event EventHandler MinBtnClick;
        /// <summary>
        /// 皮肤按钮对应的响应事件
        /// </summary>
        public event EventHandler ClothBtnClick;
        /// <summary>
        /// 最大化对应的响应事件
        /// </summary>
        public event EventHandler MaxBtnClick;
        /// <summary>
        /// 菜单按钮事件
        /// </summary>
        public event EventHandler MenuBtnClick;

        //绘制关闭图标
        private void P_CLOSE_Paint(object sender, PaintEventArgs e)
        {
            ImageDrawing.ClosePicture(sender, e);
            e.Dispose();
        }

        //绘制最小化的图标
        private void P_MIN_Paint(object sender, PaintEventArgs e)
        {
            ImageDrawing.MinPicture(sender, e);
            e.Dispose();
        }

        //绘制皮肤按钮图标
        private void P_CLOTH_Paint(object sender, PaintEventArgs e)
        {
            ImageDrawing.ClothPicture(sender, e);
            e.Dispose();
        }
        //激活颜色刺激
        private void ActiveColorChange(object sender, EventArgs e)
        {
            var theControl = (Control)sender;
            var r = (byte)(255 - theControl.ForeColor.R);
            var g = (byte)(255 - theControl.ForeColor.G);
            var b = (byte)(255 - theControl.ForeColor.B);
            theControl.BackColor = Color.FromArgb(128,r, g, b);
        }
        //关闭激活颜色
        private void P_CLOSE_MouseEnter(object sender, EventArgs e)
        {
            ((Nothing)sender).BackColor = ((Nothing)sender).BackColor = Color.Red; ;
        }

        //还原控件的背景色
        private void NoActiveColorChange(object sender, EventArgs e)
        {
            ((Nothing)sender).BackColor = Color.Transparent;
        }

        /// <summary>
        /// 重属窗口标题最好在窗口加载的时候定义一下同时将无法修改标题
        /// </summary>
        public String FormTitle
        {
            set
            {
                text = value;
                this.Invalidate();//让控件进行一次重绘
            }
            get
            {
                return text;
            }
        }
        /// <summary>
        /// 重属窗口的ICON图标
        /// </summary>
        public Icon Icon
        {
            set
            {
                icon = value;
                this.Invalidate();
            }
            get
            {
                return icon;
            }
        }
        /// <summary>
        /// 是否显示最小化按钮
        /// </summary>
        public bool MinBtn { set { P_MIN.Visible = value; this.Invalidate(); } get { return P_MIN.Visible; } }
        /// <summary>
        /// 是否显示最大化按钮
        /// </summary>
        public bool MaxBtn { set { P_MAX.Visible = value; this.Invalidate(); } get { return P_MAX.Visible; } }
        /// <summary>
        /// 是否显示换肤按钮
        /// </summary>
        public bool ClothBtn { set { P_CLOTH.Visible = value; this.Invalidate(); } get { return P_CLOTH.Visible; } }
        /// <summary>
        /// 是否显示菜单项目按钮
        /// </summary>
        public bool MenuBtn { set { P_Menu.Visible = value; this.Invalidate(); } get { return P_Menu.Visible; } }

        //public ContextMenuStrip MenuContextMenuStrip { set; get; }

        private Icon icon = null;
        private string text = string.Empty;
        //绘制ICO和标题 
        private void SystemControl_Paint(object sender, PaintEventArgs e)
        {
            //动态布局位置 控件的位置
            P_Menu.Top = P_MAX.Top = P_CLOSE.Top = P_MIN.Top = P_CLOTH.Top = 0;
            int px = P_CLOSE.Left = this.Width - P_CLOSE.Width - 5;
            if (P_MAX.Visible)
            {
                P_MAX.Left = px = px - P_MAX.Width;
            }
            if (P_MIN.Visible)
            {
                P_MIN.Left = px = px - P_MIN.Width;
            }
            if (P_Menu.Visible)
            {
                P_Menu.Left = px = px - P_Menu.Width;
            }
            if (P_CLOTH.Visible)
            {
                P_CLOTH.Left = px = px - P_CLOTH.Width;
            }
            //绘制ICO
            if (icon != null)
            {
                e.Graphics.DrawIcon(icon, new Rectangle(7, 2, 16, 16));
            }
            //绘制标题
            if (!string.IsNullOrEmpty(text))
            {
                int x = 5, y = 5;
                var p = new SolidBrush(this.ForeColor);
                if (icon != null)
                {
                    x = x + 20;
                }
                e.Graphics.DrawString(text, this.Font, p, x, y);
                p.Dispose();
            }
            e.Dispose();
        }


        /// <summary>
        /// 窗体拖动事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SystemControl_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                NativeMethods.ReleaseCaptureM();
                NativeMethods.SendMessageM(this.Parent.Handle, 0x0112, 0xF012, 0);
            }
        }

        /// <summary>
        /// 绘制窗体最大化图标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void P_MAX_Paint(object sender, PaintEventArgs e)
        {
            ImageDrawing.MaxPicture(sender, e);
            e.Dispose();
        }
        /// <summary>
        /// 绘制菜单弹出按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void P_Menu_Paint(object sender, PaintEventArgs e)
        {

            ImageDrawing.MenuPicture(sender, e);
            e.Dispose();
        }

        /// <summary>
        /// 关闭事件 动态调用事件必须调用一次判断一次
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void P_CLOSE_Click(object sender, EventArgs e)
        {
            if (CloseBtnClick != null)
                CloseBtnClick(sender, e);
        }
        /// <summary>
        /// 换肤按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void P_CLOTH_Click(object sender, EventArgs e)
        {
            if (ClothBtnClick != null)
                ClothBtnClick(sender, e);
        }

        /// <summary>
        /// 最大化按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void P_MAX_Click(object sender, EventArgs e)
        {
            if (MaxBtnClick != null)
                MaxBtnClick(sender, e);
        }

        /// <summary>
        /// 菜单按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void P_Menu_Click(object sender, EventArgs e)
        {
            if (MenuBtnClick != null)
                MenuBtnClick(sender, e);
        }
        /// <summary>
        /// 最小化按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void P_MIN_Click(object sender, EventArgs e)
        {
            if (MinBtnClick != null)
                MinBtnClick(sender, e);
        }

        private void SystemControl_ForeColorChanged(object sender, EventArgs e)
        {
            P_MAX.ForeColor = P_MIN.ForeColor = P_Menu.ForeColor = P_CLOSE.ForeColor = P_CLOTH.ForeColor = this.ForeColor;
        }

   
    }
}
